﻿Imports System.Collections.Generic
Imports System.Data
Imports System.IO 

Partial Class WebForm_AssetMaintain_Component_AssetHelp
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Me.InitDataAndShow()

        End If
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnUpload)
        Me.Visible = False
    End Sub
    Protected Sub GridView_UpFiles_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView_UpFiles.PageIndexChanged
        Me.Show()
    End Sub
    ''' <summary>
    ''' 上傳檔案
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If Not Me.FileUpload1.HasFile Then
            ' BasePage.ShowErrorMessage("此檔案不存在!!!")
        Else
            Dim FileName As String = Me.FileUpload1.FileName.Substring(0, Me.FileUpload1.FileName.LastIndexOf("."))
            Dim FileExtension As String = Me.FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."), Me.FileUpload1.FileName.Length - FileName.Length)
            If FileExtension.ToUpper <> ".PDF" Then
                'BasePage.ShowMessageForAjax(btnUpload, "本程式限傳PDF檔")
                'Me.Show()
                'Exit Sub
            End If

            Dim guidTmpSaveID As Guid = Guid.NewGuid()
            Dim FileName_New As String = FileName & FileExtension ' & guidTmpSaveID.ToString() 
            Dim sPath As String = (Server.MapPath("~/Public/"))
            sPath += FileName_New
            Me.FileUpload1.SaveAs(sPath)
            Dim row As Data.DataRow
            row = Me.DataDT.NewRow()
            CreateDataPath = Me.ResolveUrl("~/Public/" & FileName_New)
            row("FilePath") = CreateDataPath
            row("FileID") = guidTmpSaveID.ToString() & String.Format("{0}", Me.DataDT.Rows.Count + 1)
            row("FileName") = FileName_New
            Me.DataDT.Rows.Add(row)
            Me.GridView_UpFiles.DataSource = Me.DataDT
            Me.GridView_UpFiles.DataBind()
        End If
        Me.Show()
    End Sub


    ''' <summary>
    ''' 刪除檔案 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub lbtnFileDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim RowIndex As Integer = DirectCast(DirectCast(sender, LinkButton).NamingContainer, GridViewRow).RowIndex
            Dim objDataRow As DataRow = DataDT.Rows(RowIndex)
            Dim FileInfo As New System.IO.FileInfo(Server.MapPath(objDataRow("FilePath").ToString()))
            If FileInfo.Exists Then
                FileInfo.Delete()
                DataDT.Rows(RowIndex).Delete()
       
                Me.GridView_UpFiles.DataSource = DataDT
                Me.GridView_UpFiles.DataBind()
            End If
        Catch ex As Exception
            ' BasePage.ShowErrorMessage("刪除失敗")
        End Try

        Me.Show()
    End Sub
    Public Sub InitDataAndShow()
        Dim GroupBBL As New OAMS.BBL.GroupBBL
        GridView_UpFiles.DataSource = DataDT
        GridView_UpFiles.DataBind()
        If GroupBBL.GroupMemberIsExist("Admin", hiUID, dbName) Then
            GridView_UpFiles.Columns(2).Visible = True
            btnUpload.Visible = True
            FileUpload1.Visible = True
        Else
            GridView_UpFiles.Columns(2).Visible = False
            btnUpload.Visible = False
            FileUpload1.Visible = False
        End If
        Me.Show()
    End Sub
    Public Sub Show()
        Me.Visible = True
        Me.mpSearch.Show()
    End Sub
    Property CreateDataPath() As String
        Get
            Return ViewState("CreateDataPath")
        End Get
        Set(ByVal value As String)
            ViewState("CreateDataPath") = value
        End Set
    End Property
    Property DataDT() As Data.DataTable
        Get
            If ViewState("DataDT") Is Nothing Then
                Dim MyDt As New Data.DataTable
                Dim dc As New Data.DataColumn("FilePath", System.Type.[GetType]("System.String"))
                MyDt.Columns.Add(dc)
                dc = New Data.DataColumn("FileID", System.Type.[GetType]("System.String"))
                MyDt.Columns.Add(dc)
                dc = New Data.DataColumn("FileName", System.Type.[GetType]("System.String"))
                MyDt.Columns.Add(dc)
                ViewState("DataDT") = MyDt
                Dim dr As DataRow
                Dim myDirectoryInfo As New DirectoryInfo(Server.MapPath("~/Public"))

               
                If myDirectoryInfo.Exists Then

                    For Each myFileInfo As FileInfo In myDirectoryInfo.GetFiles()
                        dr = MyDt.NewRow()

                        dr("FileName") = Path.GetFileName(myFileInfo.FullName)
                        dr("FilePath") = "~/public/" + Path.GetFileName(myFileInfo.FullName)

                        MyDt.Rows.Add(dr)
                    Next
                End If




            End If
            Return ViewState("DataDT")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("DataDT") = value
        End Set
    End Property

   
    Public Property hiUID() As String
        Get
            Return ViewState("hiUID")
        End Get
        Set(ByVal value As String)
            ViewState("hiUID") = value
        End Set
    End Property
    Public Property dbName() As String
        Get
            Return ViewState("dbName")
        End Get
        Set(ByVal value As String)
            ViewState("dbName") = value
        End Set
    End Property

End Class
